from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from time import sleep,ctime
#创建浏览器驱动对象
driver = webdriver.Chrome()
# 窗口最大化
driver.maximize_window()
# # 隐形等待(全局)
# driver.implicitly_wait(5)
# try:
#     print(ctime())
#     driver.find_element_by_id("sb_form_q").send_keys("bella")
# except NoSuchElementException as e:
#     print(e)
# finally:
#     print(ctime())

# 显示等待（局部）
# 显隐结合按最长计时算
driver.implicitly_wait(3)
driver.get("http://cn.bing.com")
ob = (By.ID, "sb_form_qq")
try:
    print(ctime())
    # WebDriverWait(作用对象，作用总时间，时间间隔)
    WebDriverWait(driver, 5, 0.5).until(EC.presence_of_element_located(ob)).send_keys("bella")
except NoSuchElementException as e:
    print(e)
finally:
    print(ctime())
    driver.quit()
# bing首页，输入bella，删除最后一个字母，全部选中，复制，清楚输入框，粘贴，退出浏览器
# driver.get("http://cn.bing.com")
# elem = driver.find_element_by_id("sb_form_q")
# elem.send_keys("bella")
# sleep(2)
# elem.send_keys(Keys.BACK_SPACE)
# sleep(2)
# elem.send_keys(Keys.CONTROL, "a")
# sleep(2)
# elem.send_keys(Keys.CONTROL, "c")
# sleep(2)
# elem.clear()
# sleep(2)
# elem.send_keys(Keys.CONTROL, "v")
# sleep(2)
# elem.send_keys(Keys.ENTER)
# sleep(3)
# driver.quit()

# 数组层级定位
# driver.get("file:///C:/Users/S3214/Downloads/TableSelect.html")
# position3_2 = driver.find_element_by_id("qw")
# row3 = position3_2.find_elements_by_tag_name("tr")
# column2 = row3[2].find_elements_by_tag_name("td")[1].text
# print("三行二裂文本信息:%s" % column2)